import { run } from "./runQuirrel";
import type http from "http";
import request from "supertest";
import jwt from "jsonwebtoken";

const privateKey = `
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAnzyis1ZjfNB0bBgKFMSvvkTtwlvBsaJq7S5wA+kzeVOVpVWw
kWdVha4s38XM/pa/yr47av7+z3VTmvDRyAHcaT92whREFpLv9cj5lTeJSibyr/Mr
m/YtjCZVWgaOYIhwrXwKLqPr/11inWsAkfIytvHWTxZYEcXLgAXFuUuaS3uF9gEi
NQwzGTU1v0FqkqTBr4B8nW3HCN47XUu0t8Y0e+lf4s4OxQawWD79J9/5d3Ry0vbV
3Am1FtGJiJvOwRsIfVChDpYStTcHTCMqtvWbV6L11BWkpzGXSW4Hv43qa+GSYOD2
QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9MwIDAQABAoIBACiARq2wkltjtcjs
kFvZ7w1JAORHbEufEO1Eu27zOIlqbgyAcAl7q+/1bip4Z/x1IVES84/yTaM8p0go
amMhvgry/mS8vNi1BN2SAZEnb/7xSxbflb70bX9RHLJqKnp5GZe2jexw+wyXlwaM
+bclUCrh9e1ltH7IvUrRrQnFJfh+is1fRon9Co9Li0GwoN0x0byrrngU8Ak3Y6D9
D8GjQA4Elm94ST3izJv8iCOLSDBmzsPsXfcCUZfmTfZ5DbUDMbMxRnSo3nQeoKGC
0Lj9FkWcfmLcpGlSXTO+Ww1L7EGq+PT3NtRae1FZPwjddQ1/4V905kyQFLamAA5Y
lSpE2wkCgYEAy1OPLQcZt4NQnQzPz2SBJqQN2P5u3vXl+zNVKP8w4eBv0vWuJJF+
hkGNnSxXQrTkvDOIUddSKOzHHgSg4nY6K02ecyT0PPm/UZvtRpWrnBjcEVtHEJNp
bU9pLD5iZ0J9sbzPU/LxPmuAP2Bs8JmTn6aFRspFrP7W0s1Nmk2jsm0CgYEAyH0X
+jpoqxj4efZfkUrg5GbSEhf+dZglf0tTOA5bVg8IYwtmNk/pniLG/zI7c+GlTc9B
BwfMr59EzB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-----END PUBLIC KEY-----
`;

describe("JWT Authentication", () => {
  let quirrel: http.Server;
  let teardown: () => Promise<void>;

  beforeAll(async () => {
    const res = await run("Mock", {
      passphrases: ["root"],
      jwtPublicKey: publicKey,
    });
    quirrel = res.server;
    teardown = res.teardown;
  });

  afterAll(async () => {
    await teardown();
  });

  describe("when using a freshly signed token", () => {
    it("allows & increments usage", async () => {
      const validToken = jwt.sign({}, Buffer.from(privateKey), {
        subject: "jason web token",
        algorithm: "RS512",
      });

      await request(quirrel)
        .get("/queues")
        .auth(validToken, { type: "bearer" })
        .expect(200);

      await request(quirrel)
        .delete("/usage")
        .auth("ignored", "root")
        .expect(200, { "jason web token": 1 });
    });
  });

  describe("when using an invalid token", () => {
    it("blocks", async () => {
      const invalidToken = jwt.sign({}, Buffer.from(privateKey), {
        subject: "jason web token",
        notBefore: "1d",
        algorithm: "RS512",
      });

      await request(quirrel)
        .get("/queues")
        .auth(invalidToken, { type: "bearer" })
        .expect(401);
    });
  });
});
